package com.guigu.www.erp.dehao.controller;

import com.guigu.www.erp.common.pojo.MApply;
import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.dehao.service.DHMApplyService;
import com.guigu.www.erp.dehao.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/DHMApply")
public class DHMApplyController {
    @Autowired
    private DHMApplyService service;

    @DeleteMapping("{id}")
    public void deleteByPrimaryKey(@PathVariable Integer id) {
        service.deleteByPrimaryKey(id);
    }

    @PostMapping("/add")
    public void insertSelective(@RequestBody List<MApply> mApplys) {
        //获取编号
        Calendar now = Calendar.getInstance();
        String time="300";
        //年
        time+=now.get(Calendar.YEAR);

        //月
        String month = (now.get(Calendar.MONTH) + 1)+"";
        month=month.length()==1?"0"+month : month;
        time+=month;

        //日
        String day = now.get(Calendar.DAY_OF_MONTH)+"";
        day=day.length()==1?"0"+day : day;
        time+=day;

        //查询最大编号的方法
        String s = service.queryMaxApplyId();
        if (s==null){
            time+="0001";
        }else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str=i+"";
            if (str.length()==1){
                str="000"+str;
            }else if (str.length()==2){
                str="00"+str;
            }else if (str.length()==3){
                str="0"+str;
            }
            time+=str;
        }

        //获取当前时间
        Date date = new Date();
        String registerTime = date.toLocaleString();

        for (MApply mApply : mApplys) {
            mApply.setId(null);
            mApply.setRegisterTime(registerTime);
            mApply.setApplyId(time);
            mApply.setCheckTag("0");
            mApply.setManufactureTag("0");

            //插入数据库中
            service.insertSelective(mApply);
        }

    }

    @GetMapping("/byId")
    public MApply selectByPrimaryKey(Integer id) {
        return service.selectByPrimaryKey(id);
    }

    @GetMapping("/byIdMApplyList")
    public List<MApply> selectByPrimaryKey111(Integer[] ids) {
        List<MApply> list = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            MApply mApply = service.selectByPrimaryKey(ids[i]);
            list.add(mApply);
        }

        return list;
    }

    @PutMapping("/up")
    public void updateByPrimaryKeySelective(@RequestBody MApply mApply) {
        service.updateByPrimaryKeySelective(mApply);
    }

    @RequestMapping("/findLike0")
    public PageVo<MApply> findLikeMApply0(Integer pageNum, Integer pageSize,
                                         String productName, String startTime, String endTime,
                                         HttpServletRequest request){

        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser.getRole().getRoleId()==4){
            return null;
        }

        return service.findLikeMApply(pageNum, pageSize, productName, startTime, endTime, loginUser.getDeptId());
    }

    @RequestMapping("/findLike")
    public PageVo<MApply> findLikeMApply(Integer pageNum, Integer pageSize,String applyId,
                                         String productName, String startTime, String endTime){

        return service.queryLikeMApply(pageNum, pageSize, applyId,productName, startTime, endTime);
    }

    @GetMapping("/byApplyId")
    public List<MApply> queryByApplyId(String applyId) {
        return service.queryByApplyId(applyId);
    }

    @PutMapping("/byApplyIdUp")
    public void updateByApplyId(@RequestBody MApply mApply) {
        Date date = new Date();
        mApply.setCheckTime(date.toLocaleString());
        service.updateByApplyId(mApply);
    }

    @GetMapping("/numberCheckTag0")
    public Integer numberCheckTag0() {
        return service.numberCheckTag0();
    }

    @GetMapping("/numberCheckTag1")
    public Integer numberCheckTag1() {
        return service.numberCheckTag1();
    }

    @GetMapping("/numberCheckTag2")
    public Integer numberCheckTag2() {
        return service.numberCheckTag2();
    }
}
